package com.crm.system.service.impl.productManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.DictionaryEnum;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.common.Dictionary;
import com.crm.model.entity.productManager.CustomerMemberProductRelation;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.productManager.CustomerMemberCardServingQO;
import com.crm.service.common.DictionaryDbService;
import com.crm.service.productManager.CustomerMemberCardServingDbService;
import com.crm.system.service.productManager.CustomerMemberCardServingService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2021/4/12 16:49
 */
@Service
public class CustomerMemberCardServingServiceImpl implements CustomerMemberCardServingService {

    @Autowired
    private DictionaryDbService dictionaryDbService;
    @Autowired
    private CustomerMemberCardServingDbService customerMemberCardServingDbService;

    /***
     * @Description: 查询会员卡服务一级类型接口
     * @Param: parentId 父id
     * @Author: luojie
     * @Date: 2021/4/12 16:37
     */
    @Override
    public List<JSONObject> selectMemberCardServingDropDownList(String parentId, String memberProductRelationId) {
        List<Dictionary> dictionaryList = dictionaryDbService.selectDictionaryByTypeCodeAndParentId(DictionaryEnum.MEMBERSHIP_CARD_SERVICES.getTypeCode(),parentId);
        List<JSONObject> jsonObjectList = new ArrayList<>();
        dictionaryList.stream().forEach(dictionary -> {
            JSONObject jsonObject = new JSONObject();
            CustomerMemberProductRelation customerMemberProductRelation = customerMemberCardServingDbService.selectMemberCardServingById(memberProductRelationId);
            jsonObject.put("id",dictionary.getDictId());
            jsonObject.put("name",dictionary.getDictName());
            //判断下拉框是否选中
            if(customerMemberProductRelation!=null&&(customerMemberProductRelation.getCourseTypeId().equals(dictionary.getDictId())||
            customerMemberProductRelation.getCourseSubcategoryId().equals(dictionary.getDictId()))){
                jsonObject.put("checked",true);
            }else {
                jsonObject.put("checked",false);
            }
            jsonObjectList.add(jsonObject);
        });

        return jsonObjectList;
    }

    /***
     * @Description:保存会员卡服务配置项
     * @Param:CustomerMemberCardServingQO 保存会员卡服务配置项类
     * @Author: luojie
     * @Date: 2021/4/13 10:28
     */
    @Override
    public Boolean addMemberCardServing(CustomerMemberCardServingQO customerMemberCardServingQO) {
        Boolean result;
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CustomerMemberProductRelation customerMemberProductRelation = new CustomerMemberProductRelation();
        customerMemberProductRelation.setMemberProductId(customerMemberCardServingQO.getMemberProductId());
        customerMemberProductRelation.setServiceType(customerMemberCardServingQO.getServiceType());
        customerMemberProductRelation.setCourseTypeId(customerMemberCardServingQO.getCourseTypeId());
        customerMemberProductRelation.setCourseSubcategoryId(customerMemberCardServingQO.getCourseSubcategoryId());
        customerMemberProductRelation.setServiceCount(customerMemberCardServingQO.getServiceCount());

        //判断MemberProductRelationId为不为空，为空则为新增，不为空则为更新
        if(StringUtils.isBlank(customerMemberCardServingQO.getMemberProductRelationId())){
            customerMemberProductRelation.setMemberProductRelationId(CommonUtils.getGUID());
            customerMemberProductRelation.setCreatePeopleId(userAccount.getAccountId());
            customerMemberProductRelation.setCreateTm(new Date());
            customerMemberProductRelation.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            result = customerMemberCardServingDbService.addMemberCardServing(customerMemberProductRelation);
        }else {
            customerMemberProductRelation.setMemberProductRelationId(customerMemberCardServingQO.getMemberProductRelationId());
            customerMemberProductRelation.setUpdatePeopleId(userAccount.getAccountId());
            customerMemberProductRelation.setUpdateTm(new Date());
            result = customerMemberCardServingDbService.updateMemberCardServing(customerMemberProductRelation);
        }
        return result;
    }

    /***
     * @Description:删除会员服务产品和服务项关系表数据
     * @Param: memberProductRelationId 会员服务产品和服务项关系表主键
     * @Author: luojie
     * @Date: 2021/4/13 13:48
     */
    @Override
    public Boolean deleteMemberCardServingById(String memberProductRelationId) {
        CustomerMemberProductRelation customerMemberProductRelation = new CustomerMemberProductRelation();
        customerMemberProductRelation.setMemberProductRelationId(memberProductRelationId);
        customerMemberProductRelation.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        Boolean result = customerMemberCardServingDbService.updateMemberCardServing(customerMemberProductRelation);
        return result;
    }

    /***
     * @Description: 添加服务明细
     * @param parentId 父id
     * @param name 服务名称
     * @Author: luojie
     * @Date: 2021/4/13 16:30
     */
    @Override
    public String addServingDetail(String parentId,String name) {
        String dictId = CommonUtils.getGUID();

        //字典表中的大分类（15-会员卡服务项）
        String typeCode = DictionaryEnum.MEMBERSHIP_CARD_SERVICES.getTypeCode();
        //当前登录人账号
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        //查询最大的code号，作为新增的id号
        List<Dictionary> dictionaryList = dictionaryDbService.selectDictionaryByTypeCodeAndParentId(typeCode,parentId);

        //更新新增分类成功则添加新增服务项
        Dictionary dictionary = new Dictionary();
        dictionary.setDictId(dictId);
        dictionary.setTypeCode(typeCode);
        dictionary.setDictCode(String.valueOf(dictionaryList.size()));
        dictionary.setDictName(name);
        dictionary.setParentId(parentId);
        dictionary.setCreateTm(new Date());
        dictionary.setCreatePeopleId(userAccount.getAccountId());
        dictionaryDbService.insertDictionary(dictionary);
        return dictId;
    }
}
